import dayjs from 'dayjs';
import { TChartColor } from '@/config/color';
import { getChartListColor } from '@/utils/color';

//import { getRandomArray } from '@/utils/charts';


/** 首页 dashboard 折线图 */
export function constructInitDashboardDataset2(type: string,data:Array<number>) {
  const dateArray: Array<string> = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  const datasetAxis = {
    xAxis: {
      type: 'category',
      show: false,
      data: dateArray,
    },
    yAxis: {
      show: false,
      type: 'value',
    },
    grid: {
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
    },
  };

  if (type === 'line') {
    const lineDataset = {
      ...datasetAxis,
      color: ['#fff'],
      series: [
        {
          data: data,
          type,
          showSymbol: true,
          symbol: 'circle',
          symbolSize: 0,
          markPoint: {
            data: [
              { type: 'max', name: '最大值' },
              { type: 'min', name: '最小值' },
            ],
          },
          lineStyle: {
            width: 2,
          },
        },
      ],
    };
    return lineDataset;
  }
  const barDataset = {
    ...datasetAxis,
    color: getChartListColor(),
    series: [
      {
        data: [
          100,
          130,
          184,
          218,
          {
            value: 135,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 118,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 60,
            itemStyle: {
              opacity: 0.2,
            },
          },
        ],
        type,
        barWidth: 9,
      },
    ],
  };
  return barDataset;
}
export function constructInitDashboardDataset(type: string) {
  const dateArray: Array<string> = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  const datasetAxis = {
    xAxis: {
      type: 'category',
      show: false,
      data: dateArray,
    },
    yAxis: {
      show: false,
      type: 'value',
    },
    grid: {
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
    },
  };

  if (type === 'line') {
    
    const lineDataset = {
      ...datasetAxis,
      color: ['#fff'],
      series: [
        {
          data: [150, 230, 224, 218, 135, 147, 260],
          type,
          showSymbol: true,
          symbol: 'circle',
          symbolSize: 0,
          markPoint: {
            data: [
              { type: 'max', name: '最大值' },
              { type: 'min', name: '最小值' },
            ],
          },
          lineStyle: {
            width: 2,
          },
        },
      ],
    };
    return lineDataset;
  }
  const barDataset = {
    ...datasetAxis,
    color: getChartListColor(),
    series: [
      {
        data: [
          100,
          130,
          184,
          218,
          {
            value: 135,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 118,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 60,
            itemStyle: {
              opacity: 0.2,
            },
          },
        ],
        type,
        barWidth: 9,
      },
    ],
  };
  return barDataset;
}

/** 柱状图数据源 */
export function constructInitDataset({
  dateTime = [],
  data,
  placeholderColor,
  borderColor,
}: { dateTime: Array<string> }&{data:any} & TChartColor) {
  // const dataset: Array<Array<string>> = [['时间'], ['入库'], ['出库']];

  const timeArray = [];
  const inArray = [];
  const outArray = [];
  //console.log('constructInitDataset data',JSON.stringify(data))
  let d1=data.data1;
  let d2=data.data2;

  if(d1.length<d2.length)
  {
    d1=d2
  }
  for(let i=0;i<d1.length;i++)
  {
    let dt=d1[i].createDate;
    timeArray.push(dayjs(dt).format('YYYY-MM-DD'));
  }

  for(let i=0;i<d1.length;i++)
  {
    inArray.push(d1[i].payCount.toString())
  }
  for(let i=0;i<d2.length;i++){
    outArray.push(d2[i].payCount.toString())
  }

  const dataset = {
    color: getChartListColor(),
    tooltip: {
      trigger: 'item',
    },
    xAxis: {
      type: 'category',
      data: timeArray,
      axisLabel: {
        color: placeholderColor,
      },
      axisLine: {
        lineStyle: {
          color: getChartListColor()[1],
          width: 1,
        },
      },
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        color: placeholderColor,
      },
      splitLine: {
        lineStyle: {
          color: borderColor,
        },
      },
    },
    grid: {
      top: '5%',
      left: '25px',
      right: 0,
      bottom: '60px',
    },
    legend: {
      icon: 'rect',
      itemWidth: 12,
      itemHeight: 4,
      itemGap: 48,
      textStyle: {
        fontSize: 12,
        color: placeholderColor,
      },
      left: 'center',
      bottom: '0',
      orient: 'horizontal',
      data: ['本月', '上月'],
    },
    series: [
      {
        name: '本月',
        data: outArray,
        type: 'bar',
      },
      {
        name: '上月',
        data: inArray,
        type: 'bar',
      },
    ],
  };

  return dataset;
}

/**
 *  线性图表数据源
 *
 * @export
 * @param {Array<string>} [dateTime=[]]
 * @returns {*}
 */
export function getLineChartDataSet({
  dateTime = [],
   data,
  placeholderColor,
  borderColor,
}: { dateTime?: Array<string> }&{data?:any}& TChartColor) {

  const timeArray = [];
  const inArray = [];
  const outArray = [];
  //console.log('getLineChartDataSet',JSON.stringify(data))
  //console.log('dateTime',JSON.stringify(dateTime))
  // let dt=data.data1;
  // if(data.data2.length>data.data1.length)
  // {
  //   dt=data.data2
  // }
  let d1=data.data1._object.data1;
  let d2=data.data2._object.data2;
  let dt=d1;
  if(d2.length>d1.length)
  {
    dt=d2
  }
  for(let i=0;i<dt.length;i++)
  {
    let t=dt[i].createDate;
    timeArray.push(dayjs(t).format('DD'));
  }

  for(let i=0;i<d1.length;i++)
  {
    outArray.push(d1[i].payAmount.toString())
  }
  for(let i=0;i<d2.length;i++)
  {
    inArray.push(d2[i].payAmount.toString())
  }

  //console.log('inArray',JSON.stringify(inArray))
  //console.log('outArray',JSON.stringify(outArray))

  const dataSet = {
    color: getChartListColor(),
    tooltip: {
      trigger: 'item',
    },
    grid: {
      left: '0',
      right: '20px',
      top: '5px',
      bottom: '36px',
      containLabel: true,
    },
    legend: {
      left: 'center',
      bottom: '0',
      orient: 'horizontal', // legend 横向布局。
      data: ['本月', '上月'],
      textStyle: {
        fontSize: 12,
        color: placeholderColor,
      },
    },
    xAxis: {
      type: 'category',
      data: timeArray,
      boundaryGap: false,
      axisLabel: {
        color: placeholderColor,
      },
      axisLine: {
        lineStyle: {
          width: 1,
        },
      },
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        color: placeholderColor,
      },
      splitLine: {
        lineStyle: {
          color: borderColor,
        },
      },
    },
    series: [
      {
        name: '本月',
        data: outArray,
        type: 'line',
        smooth: false,
        showSymbol: true,
        symbol: 'circle',
        symbolSize: 8,
        itemStyle: {
          borderColor,
          borderWidth: 1,
        },
        areaStyle: {
          opacity: 0.1,
        },
      },
      {
        name: '上月',
        data: inArray,
        type: 'line',
        smooth: false,
        showSymbol: true,
        symbol: 'circle',
        symbolSize: 8,
        itemStyle: {
          borderColor,
          borderWidth: 1,
        },
      },
    ],
  };
  return dataSet;
}

/**
 * 获取饼图数据
 *
 * @export
 * @param {number} [radius=1]
 * @param {any} 数据  颜色列表colorList 项目items 数据data[{name:'xx',value:23}]
 * @returns {*}
 */
export function getPieChartDataSet({
  data,
  textColor,
  placeholderColor,
  containerColor,
//}: {data:any} & Record<string,string>) {
  }: {data:any}&Record<string,any>) {
let d=data

//console.log('d',d.data)

  return {
    color: getChartListColor(),
    tooltip: {
      //show: true,
      //trigger: 'axis',
      //position: null,
      formatter:function(param:any){
        return param.data.name+":"+param.value+"笔"
      }
    },
    grid: {
      top: '0',
      right: '0',
    },
    legend: {
      selectedMode: false,
      itemWidth: 12,
      itemHeight: 4,
      textStyle: {
        fontSize: 12,
        color: placeholderColor, //底部文字颜色
      },
      left: 'center',
      bottom: '0',
      orient: 'horizontal', // legend 横向布局。
    },
    series: [
      {
        name: '支付渠道',
        type: 'pie',
        radius: ['38%', '50%'],
        avoidLabelOverlap: true,
        selectedMode: true,
        silent: true,
        itemStyle: { //每项样式
          borderColor: containerColor,
          borderWidth: 1,
          color:function(params:any){
            return d.colorList[params.dataIndex]
          }
        },
        label: {
         normal: {
            formatter: function(params:any) {
              //console.log('label',JSON.stringify(params))
              if (params.name != '')
                return params.name + ":" + params.value + '笔';
              else
                return '';
            },
            show: true,
          },

            // formatter: function(params:any) {
            //   console.log('label',JSON.stringify(params))
            //   if (params.name != '')
            //     return params.name + ":" + params.value + '笔';
            //   else
            //     return '';
            // },


          show: true,
          position: 'center',
          formatter: ['{value|{d}%}', '{name|{b}渠道占比}'].join('\n'),
          rich: {
            value: {
              color: textColor,
              fontSize: 28,
              fontWeight: 'normal',
              lineHeight: 46,
            },
            name: {
              color: '#909399', //中间文字
              fontSize: 12,
              lineHeight: 14,
            },
          },
        },
        emphasis: {
          scale: true,
          label: {
            show: true,
            formatter: ['{value|{d}%}', '{name|{b}渠道占比}'].join('\n'),
            rich: {
              value: {
                color: textColor,
                fontSize: 28,
                fontWeight: 'normal',
                lineHeight: 46,
              },
              name: {
                color: '#04BE02',
                fontSize: 14,
                lineHeight: 14,
              },
            },
          },
        },
        labelLine: {
          show: false,
        },
        data: d.data
      },
    ],
  };
}
